#!/usr/bin/env python
# coding=utf-8
# provide DTO types for this project

import re,time

__author__ = 'vavava'

# TrackItem.track_status
TRACING  = 0
RETRYING = 0
FAILED   = 0

class TrackItem(object):
    """ Tracking summary """
    def __init__(self,ID=-1,Name="",TrackTime=time.localtime(),
                 Description="",Location="",Shipment_ID=-1,
                 Created=time.localtime(),Modified=time.localtime()):
        self.Name        = Name
        self.TrackTime   = TrackTime
        self.Description = Description
        self.Location    = Location
        self.Created     = Created
        self.Modified    = Modified
        self.ID          = ID
        self.Shipment_ID = Shipment_ID

        # status
        self.last_tracking_time=None
        self.last_update_time=None
        self.track_status=TRACING

    def __hash__(self):
        """
        define for dic key
        hash by Name,TrackTime,Description,Location
        """
        key = "%s%s%s%s"%(self.Name,self.TrackTime,self.Description,self.Location)
        return key.__hash__()

    def __eq__(self, other):
        """ define for dic key """
        return self.Name == other.Name\
                and self.TrackTime   == other.TrackTime\
                and self.Description == other.Description\
                and self.Location    == other.Location

    @staticmethod
    def parse(item_name="",text=None):
        """ parse from html fragment
            return TrackItem
         """
        try:
            if text:
                reg_str=r'<td[^>]*>(?P<content>.*?)(<span[^<]*?</span>)*</td>'
                reg = re.compile(reg_str,re.X)
                match = reg.findall(text)
                if match:
                    t1=time.strptime(match[0][0], "%d/%m/%y %H:%M")
                    return TrackItem(
                        Name=item_name,
                        TrackTime=t1,
                        Description=match[1][0],
                        Location=match[2][0],
                        Created=time.localtime(time.time()),
                        Modified=None
                    )
        except Exception as e:
            return None
        return None

class TraceItemList(object):
    def __init__(self):
        self.initialised=False
        self.items_all=[]

    @staticmethod
    def getItemList():
        """
        get list from "http://fastrackexpress.com.au/test.php?LIST=POST"
        and return  ItemList
        """
        from vavava.httpclient import HttpClient
        import  re
        try:
            item = TraceItemList()
            client=HttpClient()
            html=client.Get(r"http://fastrackexpress.com.au/test.php?LIST=POST")
            htmlStr=html.decode('utf-8')
            reg_str=r'<td>(?P<id>[^<]*)</td>'
            reg=re.compile(reg_str,re.X)
            idList=reg.findall(htmlStr)
            if idList and idList[0] == 'Courier_Reference':
                item.items_all = idList[1:]
            else:
                item.items_all = idList
            item.initialised = True
            return item
        except:
            return None

if __name__ == "__main__":
    print(TraceItemList.getItemList().items_all)
